package com.yunxin.core.wechat.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunxin.common.exception.BusinessException;
import com.yunxin.core.admin.domain.AdminTeam;
import com.yunxin.core.admin.domain.AdminUserTeamShip;
import com.yunxin.core.admin.service.AdminTeamService;
import com.yunxin.core.admin.service.AdminUserTeamShipService;
import com.yunxin.core.customer.dto.AddWechatInfoTeamDto;
import com.yunxin.core.customer.dto.TeamPageDto;
import com.yunxin.core.customer.dto.WechatInfoPageDto;
import com.yunxin.core.wechat.domain.WechatInfo;
import com.yunxin.core.wechat.domain.WechatInfoTeam;
import com.yunxin.core.wechat.service.WechatInfoService;
import com.yunxin.core.wechat.mapper.WechatInfoMapper;
import com.yunxin.core.wechat.service.WechatInfoTeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【wechat_info(客户微信授权详情表)】的数据库操作Service实现
 * @createDate 2024-05-25 16:18:02
 */
@Slf4j
@DS("crm")
@Service
public class WechatInfoServiceImpl extends ServiceImpl<WechatInfoMapper, WechatInfo>
        implements WechatInfoService {

    @Autowired
    private WechatInfoTeamService wechatInfoTeamService;
    @Autowired
    private AdminTeamService teamService;
    @Autowired
    private AdminUserTeamShipService adminUserTeamShipService;

    @Override
    public Page<WechatInfo> wechatInfoPageQuery(WechatInfoPageDto command) {
        LambdaQueryWrapper<WechatInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(command.getCorpName()), WechatInfo::getCorpName, command.getCorpName())
                .orderByDesc(WechatInfo::getCreateTime);
        Page<WechatInfo> page = new Page<>(command.getPageNum(), command.getPageSize());
        this.page(page, queryWrapper);
        return page;
    }

    @Override
    public Page<AdminTeam> teamPageQuery(TeamPageDto command, Long userId) {
        Page<AdminTeam> page = new Page<>(command.getPageNum(), command.getPageSize());
        LambdaQueryWrapper<AdminUserTeamShip> teamShipLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teamShipLambdaQueryWrapper.eq(AdminUserTeamShip::getUserMerchantId, userId);
        List<AdminUserTeamShip> teamShipList = adminUserTeamShipService.list(teamShipLambdaQueryWrapper);
        Set<Integer> teamIdList = teamShipList.stream().map(i -> i.getTeamId()).collect(Collectors.toSet());
        if (ObjectUtils.isEmpty(teamShipList)){
            return page;
        }
        LambdaQueryWrapper<AdminTeam> teamLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teamLambdaQueryWrapper.like(!ObjectUtils.isEmpty(command.getTeamName()), AdminTeam::getTeamName, command.getTeamName())
                .in(AdminTeam::getId,teamIdList)
                .orderByDesc(AdminTeam::getCreateTime);
        teamService.page(page, teamLambdaQueryWrapper);
        List<AdminTeam> records = page.getRecords();
        if (!ObjectUtils.isEmpty(records)) {
            List<Integer> teamIds = records.stream().map(i -> i.getId()).toList();
            LambdaQueryWrapper<WechatInfoTeam> infoTeamLambdaQueryWrapper = new LambdaQueryWrapper<>();
            infoTeamLambdaQueryWrapper.in(WechatInfoTeam::getTeamId, teamIds);
            List<WechatInfoTeam> infoTeamList = wechatInfoTeamService.list(infoTeamLambdaQueryWrapper);
            if (!ObjectUtils.isEmpty(infoTeamList)) {
                Set<Long> wechatInfoIds = infoTeamList.stream().map(i -> i.getWechatInfoId()).collect(Collectors.toSet());
                Map<Long, Integer> infoTeamMap = infoTeamList.stream().collect(Collectors.toMap(obj -> obj.getWechatInfoId(), obj -> obj.getTeamId()));
                LambdaQueryWrapper<WechatInfo> wechatInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                wechatInfoLambdaQueryWrapper.in(WechatInfo::getId, wechatInfoIds);
                List<WechatInfo> wechatInfoList = this.list(wechatInfoLambdaQueryWrapper);
                Map<Integer, AdminTeam> teamMap = records.stream().collect(Collectors.toMap(obj -> obj.getId(), obj -> obj));
                for (WechatInfo wechatInfo : wechatInfoList) {
                    Integer teamId = infoTeamMap.get(wechatInfo.getId());
                    AdminTeam team = teamMap.get(teamId);
                    team.setWechatInfoId(wechatInfo.getId());
                    team.setCorpName(wechatInfo.getCorpName());
                }
            }
        }
        return page;
    }

    @Override
    public List<AdminTeam> teamListQuery(Long userId) {
        LambdaQueryWrapper<AdminUserTeamShip> teamShipLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teamShipLambdaQueryWrapper.eq(AdminUserTeamShip::getUserMerchantId, userId);
        List<AdminUserTeamShip> teamShipList = adminUserTeamShipService.list(teamShipLambdaQueryWrapper);
        Set<Integer> teamIdList = teamShipList.stream().map(i -> i.getTeamId()).collect(Collectors.toSet());
        if (ObjectUtils.isEmpty(teamShipList)){
            return null;
        }
        LambdaQueryWrapper<AdminTeam> teamLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teamLambdaQueryWrapper.in(AdminTeam::getId,teamIdList)
                .orderByDesc(AdminTeam::getCreateTime);
        List<AdminTeam> list = teamService.list(teamLambdaQueryWrapper);
        if (!ObjectUtils.isEmpty(list)) {
            List<Integer> teamIds = list.stream().map(i -> i.getId()).toList();
            LambdaQueryWrapper<WechatInfoTeam> infoTeamLambdaQueryWrapper = new LambdaQueryWrapper<>();
            infoTeamLambdaQueryWrapper.in(WechatInfoTeam::getTeamId, teamIds);
            List<WechatInfoTeam> infoTeamList = wechatInfoTeamService.list(infoTeamLambdaQueryWrapper);
            if (!ObjectUtils.isEmpty(infoTeamList)) {
                Set<Long> wechatInfoIds = infoTeamList.stream().map(i -> i.getWechatInfoId()).collect(Collectors.toSet());
                Map<Long, Integer> infoTeamMap = infoTeamList.stream().collect(Collectors.toMap(obj -> obj.getWechatInfoId(), obj -> obj.getTeamId()));
                LambdaQueryWrapper<WechatInfo> wechatInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                wechatInfoLambdaQueryWrapper.in(WechatInfo::getId, wechatInfoIds);
                List<WechatInfo> wechatInfoList = this.list(wechatInfoLambdaQueryWrapper);
                Map<Integer, AdminTeam> teamMap = list.stream().collect(Collectors.toMap(obj -> obj.getId(), obj -> obj));
                for (WechatInfo wechatInfo : wechatInfoList) {
                    Integer teamId = infoTeamMap.get(wechatInfo.getId());
                    AdminTeam team = teamMap.get(teamId);
                    team.setWechatInfoId(wechatInfo.getId());
                    team.setCorpName(wechatInfo.getCorpName());
                    team.setCorpid(wechatInfo.getCorpid());
                }
            }
        }
        return list;
    }

    @Override
    public void addWechatInfoTeam(AddWechatInfoTeamDto command) {
        LambdaQueryWrapper<WechatInfoTeam> infoTeamLambdaQueryWrapper = new LambdaQueryWrapper<>();
        infoTeamLambdaQueryWrapper.eq(WechatInfoTeam::getTeamId, command.getTeamId());
        long count = wechatInfoTeamService.count(infoTeamLambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException("该团队已经关联企业微信,不能重复关联");
        }
        WechatInfoTeam wechatInfoTeam = new WechatInfoTeam();
        wechatInfoTeam.setTeamId(command.getTeamId());
        wechatInfoTeam.setWechatInfoId(command.getWechatInfoId());
        wechatInfoTeamService.save(wechatInfoTeam);
    }
}




